package com.dc.designPattern._18_memento.extend.extend2;

import lombok.Data;
import lombok.SneakyThrows;

/**
 * Description: 备忘录模式扩展_原始角色
 * @author: gdc
 * @date: 2021/7/8
 * @version 1.0
 */
@Data
public class Originator implements Cloneable {

    /**
     * 状态
     */
    private String state;

    public Originator(String state) {
        this.state = state;
    }

    /**
     * 备份对象，在原类的基础上，添加对当前对象的引用，达到对当前对象备份的目的
     */
    private Originator backup;

    /**
     * 创建一个备忘录
     */
    public void createMemento() {
        this.backup = this.clone();
    }

    /**
     * 通过备忘录对象回复原来的状态
     */
    public void restoreMemento() {
        this.state = this.backup.getState();
    }

    @SneakyThrows
    @Override
    protected Originator clone() {
        return (Originator) super.clone();
    }
}
